<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<html:header title="新增收款单">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">

    </style>

    <script>
        jQuery(function () {
            getByType("${baseUrl}",ORDER_TYPE.SKD.type, $("[name='no']"));


            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId");
            ajaxSelect("${baseUrl}user/user/getAll.action","userId");
            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId");


            if ("${customId}" != "") {
                processCustom("${customId}");
                $("[name='custom']").addClass("readonly");
                $("#selectCustom").hide();
            }


            $("[name='date']").val(GMTToDate(new Date()));
            $("[name='waitAmt']").val(getPrice("0"));
            $("[name='thisAmt']").val(getPrice("0"));
            $("[name='discount']").val(getPrice("0"));
            $("[name='totalAmt']").val(getPrice("0"));

            $("#selectCustom").click(function () {
                openBigHtmlDialog("选择供应商", "${baseUrl}select/selectCustom.action");
            });

            $("#cancel").click(function () {
                closeSubIframe();
            });

            $(".save").click(function (e) {
                $("[lay-filter='save']").click();
            });

            $("[name='thisAmt']").blur(function () {
                let thisAmt = $("[name='thisAmt']").val();
                let discount = $("[name='discount']").val();
                $("[name='thisAmt']").val(getPrice(thisAmt));

                $("[name='totalAmt']").val(getPrice(parseFloat(thisAmt)+parseFloat(discount)));
            });

            $("[name='discount']").blur(function () {
                let thisAmt = $("[name='thisAmt']").val();
                let discount = $("[name='discount']").val();
                $("[name='discount']").val(getPrice(discount));
                $("[name='totalAmt']").val(getPrice(parseFloat(thisAmt)+parseFloat(discount)));
            });

        });

        layui.use(['form', 'table', 'laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;

            laydate.render({
                elem: '#date'
            });

            form.verify({
                storeId: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '门店不能为空';
                    }
                },
                custom: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '客户不能为空';
                    }
                },
                userId: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '经办人不能为空';
                    }
                },
                date: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '收款日期不能为空';
                    }
                },
                settAccountId:function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '结算账户不能为空';
                    }
                },
                thisAmt:function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == null || value==""){
                        return '本次收款不能为空';
                    }

                    if(parseFloat(value) == 0){
                        return '本次收款不能为0';
                    }
                },
            });


            form.on('submit(save)',function (d) {
                let data = d.field;
                delete data.custom;
                data.date = genDate(data.date);
                data.image = getImageNames();
                post("${baseUrl}funds/arrearsOrder/add.action",data, function (res) {
                    closeAll();
                    if (res.code == 1){
                        refreshMainIframe("${baseUrl}funds/arrearsWait.action");
                        refreshMainIframe("${baseUrl}funds/arrearsWaitDetail.action?customId="+data.customId);
                        closeSubIframe();
                    }else{
                        errTip(res.msg);
                    }
                });
                return false;
            });
        });

        function getSelectCustom(data) {
            $(window).focus();

            processCustom(data.id);
        }

        function processCustom(customId) {
            if (!emptyEntity(customId)) {
                get("${baseUrl}funds/arrearsWait/getMapByCustom.action?customId="+customId, function (res) {
                    $("[name='custom']").val(res.data.name);
                    $("[name='customId']").val(res.data.id);

                    let waitIn = parseFloat(res.data.waitIn);
                    if (isNaN(waitIn)) {
                        waitIn = 0;
                    }
                    waitIn += parseFloat(res.data.init_amt);
                    $("[name='waitAmt']").val(getPrice(waitIn));
                });
            }
        }
    </script>

</html:header>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">



        <h2 class="tableBeforeTitle">收款单</h2>
        <div style="height: 4px;background-color: #bbbbbb;width: 100%;margin: 20px 0 2px 0;"></div>
        <div style="height: 1px;width: 100%;background-color: #dddddd;margin-bottom: 10px;"></div>
        <div style="height: 10px;"></div>

        <form class="layui-form" action="" lay-filter="saleOrder">
            <button style="display: none;" lay-submit lay-filter="save" class="layui-btn">保存</button>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-inline">
                        <input type="text" id="no" name="no" class="layui-input readonly" readonly>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">门店</label>
                    <div class="layui-input-inline">
                        <select name="storeId" lay-verify="storeId">
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客户</label>
                    <div class="layui-input-inline" style="position: relative;">
                        <input type="text" id="custom" name="custom" lay-filter="custom" class="layui-input" readonly>
                        <input type="hidden" name="customId">
                        <div style="position: absolute;top: 10px; right: 10px;">
                            <i class="layui-icon layui-icon-more" id="selectCustom"></i>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">经手人</label>
                    <div class="layui-input-inline">
                        <select name="userId" lay-verify="userId" lay-filter="userId">
                        </select>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">收款日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="date" id="date" lay-verify="date" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">结算账户</label>
                    <div class="layui-input-inline">
                        <select name="settAccountId" lay-verify="settAccountId"></select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">应收金额</label>
                    <div class="layui-input-inline">
                        <input type="number" name="waitAmt" class="layui-input readonly" readonly>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">本次收款</label>
                    <div class="layui-input-inline">
                        <input type="number" name="thisAmt" lay-verify="thisAmt" class="layui-input">
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">优惠金额</label>
                    <div class="layui-input-inline">
                        <input type="number" name="discount" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">合计金额</label>
                    <div class="layui-input-inline">
                        <input type="number" name="totalAmt" class="layui-input readonly" readonly>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" name="remark"></textarea>
                </div>
            </div>
        </form>

        <div class="layui-form-item" style="padding-top: 20px;">
            <label class="layui-form-label">图片附件</label>
            <html:imageAnnex type="arrearsOrder" update="false"
                             uploadUrl="${baseUrl}funds/arrearsOrder/uploadImg.action"
                             updateUrl="${baseUrl}funds/arrearsOrder/updateImage.action"/>
        </div>

        <hr>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <a class="layui-btn save">保存</a>
                <a class="layui-btn layui-btn-primary" id="cancel">取消</a>
            </div>
        </div>
    </div>
</div>

</body>
<html:loading/>
</html>